package com.jingogo.service.impl;

import com.jingogo.common.BusinessException;
import com.jingogo.entity.CartItem;
import com.jingogo.mapper.CartItemMapper;
import com.jingogo.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartItemMapper cartItemMapper;

    @Override
    public void addToCart(CartItem item) {
        if (item.getUserId() == null || item.getProductId() == null) throw new BusinessException("参数不完整");
        cartItemMapper.insert(item);
    }

    @Override
    public void updateCart(CartItem item) {
        if (item.getId() == null) throw new BusinessException("购物车项ID不能为空");
        cartItemMapper.update(item);
    }

    @Override
    public void removeFromCart(Long cartItemId) {
        cartItemMapper.deleteById(cartItemId);
    }

    @Override
    public List<CartItem> listCart(Long userId) {
        return cartItemMapper.selectByUserId(userId);
    }
} 